package dhz.storage.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import net.bytebuddy.dynamic.scaffold.MethodGraph;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

/**
 * 用来表示一个用户的全部上传任务
 * */
public class UserUploadTasks {
    private TreeMap<String, UploadTask> uploadTasks;

    public UserUploadTasks(){
        this.uploadTasks = new TreeMap<>();
    }

    public UserUploadTasks(TreeMap<String, UploadTask> read){
        this.uploadTasks = read;
    }

    public void addTask(String id, UploadTask t){
        uploadTasks.put(id, t);
    }

    public List<UploadTask> toList(){
        LinkedList<UploadTask> result = new LinkedList<>();
        for(String s:uploadTasks.keySet()){
            result.addLast(uploadTasks.get(s));
        }
        return result;
    }

    public int size(){
        return this.uploadTasks.size();
    }

    public void update(String id, long f, long l){
        UploadTask t = uploadTasks.get(id);
        if(t == null)return;
        t.update(f, l);
        if(t.getFinished() != t.getTotalSize())uploadTasks.put(id, t);
        else uploadTasks.remove(id);
    }

    public void deleteTask(String id){
        this.uploadTasks.remove(id);
    }
}
